package com.study.common;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

/**全局系统工具类
 * @author SMILE
 *
 */
public class AppUtils {
    /**判断一个请求是否是jQuery的ajax请求
     * @param request
     * @return
     */
    public static boolean isAjax(HttpServletRequest request) {
        return "XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"));
    }

    /**获取指定日期开始的日历对象
     * @param year
     * @param week
     * @return
     */
    public static Calendar getCalendar(Integer year, Integer week) {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(Calendar.YEAR, year);
        c.set(Calendar.WEEK_OF_YEAR, week);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONDAY), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return c;
    }

    public static Calendar getCalendar() {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONDAY), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return c;
    }

    /**生成日历
     * @return
     */
    public static List<List<Object>> getCal() {
        List<List<Object>> days = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        // 把日期的当前日期设置为1号
        cal.set(Calendar.DATE, 1);
        int num = cal.get(Calendar.DAY_OF_WEEK);
        int maxDay = cal.getActualMaximum(Calendar.DATE);
        int day = 1;
        DateFormat df = new SimpleDateFormat("yyyy-MM-");
        String dm = df.format(cal.getTime());
        List<Object> dayline = new ArrayList<>();
        while (day <= maxDay) {
            for (int i = 0; i < num - 1 && day == 1; i++) {
                dayline.add(null);
            }
            dayline.add(new Object[]{dm + day, day});
            num++;
            if (num > 7) {
                num = 1;
                days.add(dayline);
                dayline = new ArrayList<>();
            }
            day++;

        }
        return days;
    }

    /** 使用 Map按key进行排序
     * @param map
     * @return
     */
    public static <T> Map<Integer, T> sortMapByIntKey(Map<Integer, T> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Map<Integer, T> sortMap = new TreeMap<>(Comparator.reverseOrder());
        sortMap.putAll(map);
        return sortMap;
    }

    /**获取客户端的IP地址
     * @param request
     * @return
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
